import SoundManager = Laya.SoundManager;
import Handler = Laya.Handler;
import GameFacade from "../GameFacade";

export class SoundsManager {
    private _musicChannel: Laya.SoundChannel = null;
    private _soundChannelList: any;
    private _soundVolume: number = 100;
    private _musicVolume: number = 100;
    private _isBlur: boolean = false;
    private _musicBgName: string = null;

    constructor() {
        this._soundChannelList = {};
    }

    /** 用来存是否关闭了音效 的key*/
    public static IS_CLOSE_SOUND = "IS_CLOSE_SOUND";

    /** 用来存是否关闭了背景音乐 的key*/
    public static IS_CLOSE_MUSIC = "IS_CLOSE_MUSIC";


    /** 是否关闭了bgm sound*/
    public get IsCloseBGM(): boolean {
        return window.gameFacade.SDKMng.SDK.GetLocalData(SoundsManager.IS_CLOSE_SOUND, "0") == "1";
    }

    public set IsCloseBGM(value: boolean) {
        let resValue = value ? "1" : "0";
        window.gameFacade.SDKMng.SDK.SaveLocalData(SoundsManager.IS_CLOSE_SOUND, resValue);
        if (value) {
            this.stopMusic();
        } else {
            this.playMusic(this._musicBgName);
        }
    }

    /** 是否关闭了音效 sound*/
    public get IsCloseSound(): boolean {
        return window.gameFacade.SDKMng.SDK.GetLocalData(SoundsManager.IS_CLOSE_MUSIC, "0") == "1";
    }

    public set IsCloseSound(value: boolean) {
        let resValue = value ? "1" : "0";
        window.gameFacade.SDKMng.SDK.SaveLocalData(SoundsManager.IS_CLOSE_MUSIC, resValue);
    }
    /**
     * 
     * @param v 设置背景音乐
     */
    public setMusicVolume(v: number): void {
        this._musicVolume = v;
        this._musicChannel.volume = this._musicVolume / 100;
    }

    /**
     * 
     * @param v 设置音效
     */
    public setSoundVolume(v: number): void {
        this._soundVolume = v;
        this.updateSoundVolume(v / 100);
    }

    /**
     * 获得背景音乐的声音大小
     */
    public getMusicVolume(): number {
        return this._musicVolume;
    }

    /**
     * 获得音效的声音大小
     */
    public getSoundVolume(): number {
        return this._soundVolume;
    }

    /**
     * 
     * @param str 背景音乐名称
     */
    public playMusic(str: string): void {

        if (this._musicChannel) {
            this._musicChannel.stop();
            Laya.SoundManager.destroySound(this._musicChannel.url);
        }

        this._musicBgName = str;

        if (this.IsCloseBGM) {
            return
        }
        
        this._musicChannel = SoundManager.playSound("sounds/SceneSound/" + str + ".mp3", 0);
        if (this._musicChannel == null) return;
        this._musicChannel.volume = this._musicVolume / 100;
    }

    /**
     * 
     * @param head 是按钮还是鱼
     * @param str 对应的音效
     */
    public playSound(str: string): void {
        var soundName: string = "sounds/CommonSound/" + str;
        if (this._isBlur || this._soundVolume == 0) return;
        var channel: Laya.SoundChannel;
        //关闭了音效
        if (this.IsCloseSound) {
            return;
        }
        channel = SoundManager.playSound(soundName + ".mp3", 1, new Handler(this, () => { this.onComplete(soundName) }));
        if (channel) {
            channel.volume = this._soundVolume / 100;
        }
        else {
            return;
        }
        if (!this._soundChannelList[soundName])
            this._soundChannelList[soundName] = new Array<Laya.SoundChannel>();
        this._soundChannelList[soundName].push(channel);
    }

    /**
     * 
     * @param str 播放完成的那个音效
     */
    private onComplete(str: string): void {
        Laya.SoundManager.destroySound(str);
    }

    /**
     * 停止音乐
     */
    public stopMusic(): void {
        if (this._musicChannel) {
            this._musicChannel.stop();
            this._musicChannel.offAll();
            this._musicChannel = null;
        }
    }

    /**
     * 停止音效
     */
    public stopSound(): void {
        for (const key in this._soundChannelList)
            this.stopSoundName(key);
    }

    /**
      * 更新声音音量
      * @param v 
      */
    private updateSoundVolume(v: number): void {
        for (const key in this._soundChannelList) {
            if (this._soundChannelList.hasOwnProperty(key)) {
                var array: Array<laya.media.SoundChannel> = this._soundChannelList[key];
                for (let i = 0; i < array.length; i++) {
                    if (array[i])
                        array[i].volume = v;
                }
            }
        }
    }

    /**
     * 根据声音名字关闭声音
     * @param soundName 
     */
    public stopSoundName(soundName: string): void {
        if (!soundName) return;
        if (this._soundChannelList[soundName]) {
            var array = this._soundChannelList[soundName];
            for (let i = 0; i < array.length; i++) {
                if (array[i]) {
                    array[i].stop();
                    array[i].offAll();
                    array[i] = null;
                }
            }
            this._soundChannelList[soundName].length = 0;
        }
        Laya.SoundManager.destroySound(soundName);
    }

    /**
     * 停止所有的声音
     */
    public stopAll(): void {
        SoundManager.stopAll()
    }

    /**
     * 暂停背景音乐
     */
    public pauseMusic(): void {
        this._musicChannel && this._musicChannel.pause();
    }

    /**
     * 继续播放背景音乐
     */
    public resumeMusic(): void {
        if (this._musicChannel)
            this._musicChannel.resume();
        else
            this.playMusic(this._musicBgName);
        // this._musicChannel && this._musicChannel.resume();
    }
    /**
     * 前后台切换
     */
    private _visibilityChange() {
        if (Laya.stage.isVisibility) {
            this._stageOnFocus();
        } else {
            this._stageOnBlur();
        }
    }

    /**
     * 失去焦点
     */
    private _stageOnBlur() {
        this._isBlur = true;
        this.pauseMusic();
        this.stopSound();
    }

    /**
     * 获取焦点
     */
    private _stageOnFocus() {
        this._isBlur = false;
        this.resumeMusic();
    }
}